home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 420_01 / defs.h < prev    next >
C/C++ Source or Header  |  1994-02-21  |  4KB  |  144 lines

  1. /*
  2. //    defs.h -- $@4pK\E*$JDj5A$HA4%U%!%$%k6&DL$JItJ,(J
  3. //
  4. //        created    in 2/11/1994
  5. */
  6.  
  7. #ifndef    _DEFS_H_
  8. #define    _DEFS_H_
  9.  
  10. #include    <stdio.h>
  11. #include    <setjmp.h>
  12.  
  13. /* 1. Define a bitwidth of each integer correctly according to your system. */
  14. typedef    short int    int16;
  15. typedef long int     int32;
  16. typedef unsigned int     uint32;
  17.  
  18. /* 2. Choose a correct byte-order. */
  19. #define    LITTLEENDIAN
  20. /*#define    BIGENDIAN*/
  21.  
  22. /* 3. What character is used as a directory separator in your OS? Choose the correct one. */
  23. #define    DSEPARATOR    '/'
  24. /*#define    DSEPARATOR    '\\' */
  25.  
  26. /* Ok. That's all. (maybe) */
  27.  
  28. #define    error(Message)    {\
  29.     fprintf( stderr, "%s: " Message "\n", Program, File );\
  30.     longjmp( ErrorEntry, 1 ); }
  31.  
  32. #define    AllocateScreen()    {\
  33.     if ( Screen != NULL ) free (Screen);\
  34.     if ( (Screen = (int16*)malloc ((Size = Width * Height * 2) + 256)) == NULL )\
  35.     error ("%s: Insufficient memory"); }
  36.  
  37. #define    AllocateBuffer()    {\
  38.     if ( (BitBuffer = (unsigned char*)malloc (BitBufferSize)) == NULL )\
  39.         error ("%s: Insufficient memory");\
  40.     BitLength = 0;\
  41.     BitCache = 0;\
  42.     BitBufferLength = BitBufferSize; }
  43.  
  44. #define    AllocateBufferForWriting()    {\
  45.     if ( (BitBuffer = (unsigned char*)malloc (BitBufferSize)) == NULL )\
  46.         error ("%s: Insufficient memory");\
  47.     BitLength = 0;\
  48.     BitCache = 0;\
  49.     BitBufferLength = 0; }
  50.  
  51. #define    ReverseByteOrder()    {\
  52.     char    *p = (char*)Screen, c;\
  53.     int     i;\
  54.     for ( i = 0; i < Size; i += 2 ) {\
  55.     c = p[i]; p[i] = p[i + 1]; p[i + 1] = c; } }
  56.  
  57. #define    BitBufferSize    32768
  58.  
  59. #define    BitLoad(Length,Result)    {\
  60.     register uint32     Code;\
  61.     register int     TakenCount;\
  62.     int     ShiftCount, DemandLength;\
  63.     DemandLength = Length;\
  64.     Code = BitCache;\
  65.     ShiftCount = (DemandLength <= BitLength) ? 32 - DemandLength : 32 - BitLength;\
  66.     TakenCount = 32 - ShiftCount;\
  67.     Code >>= ShiftCount;\
  68.     BitCache <<= TakenCount;\
  69.     DemandLength -= TakenCount;\
  70.     BitLength -= TakenCount;\
  71.     if ( DemandLength > 0 ) {\
  72.     if ( BitBufferLength == BitBufferSize )\
  73.         BitBufferLoad();\
  74.     BitCache = (unsigned int)BitBuffer[BitBufferLength]\
  75.                | ((unsigned int)BitBuffer[BitBufferLength + 1] << 8)\
  76.            | ((unsigned int)BitBuffer[BitBufferLength + 2] << 16)\
  77.            | ((unsigned int)BitBuffer[BitBufferLength + 3] << 24);\
  78.         BitBufferLength += 4;\
  79.     Code <<= DemandLength;\
  80.     BitLength = 32 - DemandLength;\
  81.     Code |= BitCache >> BitLength;\
  82.     BitCache <<= DemandLength;\
  83.     }\
  84.     Result = Code; }
  85.  
  86. #define    BitWrite(Length,BitPattern)    {\
  87.     register int     ShiftCount;\
  88.     int     BitShift, RestCount, DemandLength;\
  89.     DemandLength = Length;\
  90.     RestCount = 32 - BitLength;\
  91.     ShiftCount = (DemandLength <= RestCount) ? DemandLength : RestCount;\
  92.     BitShift = (DemandLength > ShiftCount) ? DemandLength - ShiftCount : 0;\
  93.     BitCache <<= ShiftCount;\
  94.     BitCache |= (BitPattern) >> BitShift;\
  95.     DemandLength -= ShiftCount;\
  96.     BitLength += ShiftCount;\
  97.     if ( DemandLength > 0 ) {\
  98.     if ( BitBufferLength == BitBufferSize )\
  99.         BitBufferFlush();\
  100.     BitBuffer[BitBufferLength++] = BitCache & 0xff;\
  101.     BitBuffer[BitBufferLength++] = (BitCache >> 8) & 0xff;\
  102.     BitBuffer[BitBufferLength++] = (BitCache >> 16) & 0xff;\
  103.     BitBuffer[BitBufferLength++] = (BitCache >> 24) & 0xff;\
  104.     BitCache = (BitPattern);\
  105.     BitLength = DemandLength; } }
  106.  
  107. #define    UNKNOWN    0
  108. #define    ALPHA    1
  109. #define    BETA    2
  110. #define    GAMMA    3
  111. #define    DELTA    4
  112. #define    PIC    5
  113. #define    MAG    6
  114. #define    MAKI    7
  115. #define    PI    8
  116. #define    ML1    9
  117. #define    PPM    10
  118. #define    PBM    11
  119. #define    NumberofFormats    12
  120.  
  121. extern int16    *Screen;
  122. extern int32    Width, Height, Size;
  123. extern FILE    *fp;
  124. extern jmp_buf    ErrorEntry;
  125. extern char    *Program, *File;
  126. extern unsigned char    *BitBuffer;
  127. extern uint32     BitCache;
  128. extern int     BitLength;
  129. extern int     BitBufferLength;
  130. extern unsigned char    BitField[];
  131. extern int     MaxColorGuaranteed;
  132. extern int16    Red[], Green[], Blue[], Palette[];
  133. extern char    *Extension[];
  134. extern char    *Format[];
  135.  
  136. extern void    BitBufferLoad();
  137. extern void    BitBufferFlush();
  138. extern void    BitBufferLastFlush();
  139. extern int     istrcmp( char *s1, char *s2 );
  140. extern int     InferFormat( char *File );
  141. extern void    Initialize();
  142.  
  143. #endif /* _DEFS_H_ */
  144.